/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/<a href="https://www.kaaction.com">KAA</a>"><a href="https://www.kaaction.com">KAA</a></a> All rights reserved.
 */
package com.kaaction.modules.ka.web;


import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.DateUtils;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.common.utils.excel.ExportExcel;
import com.kaaction.modules.ka.entity.ProductOutDetail;
import com.kaaction.modules.ka.entity.Statistics;
import com.kaaction.modules.ka.service.ProductOutDetailService;

/**
 * 货品出库明细Controller
 * @author huanghaiming
 * @version 2017-04-26
 */
@Controller
@RequestMapping(value = "${adminPath}/ka/productOutDetail")
public class ProductOutDetailController extends BaseController {

	@Autowired
	private ProductOutDetailService productOutDetailService;
	
	@ModelAttribute
	public ProductOutDetail get(@RequestParam(required=false) String id) {
		ProductOutDetail entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = productOutDetailService.get(id);
			entity.setReturnDate(new Date());
		}
		if (entity == null){
			entity = new ProductOutDetail();
		}
		return entity;
	}
	
	@RequiresPermissions("ka:productOutDetail:view")
	@RequestMapping(value = {"list", ""})
	public String list(ProductOutDetail productOutDetail, HttpServletRequest request, HttpServletResponse response, Model model) {
		Statistics statistics =productOutDetailService.selStatistics(productOutDetail);
		model.addAttribute("statistics", statistics);
		Page<ProductOutDetail> page = productOutDetailService.findPage(new Page<ProductOutDetail>(request, response), productOutDetail); 
		model.addAttribute("page", page);
		return "modules/ka/productOutDetailList";
	}

	@RequiresPermissions("ka:productOutDetail:view")
	@RequestMapping(value = "form")
	public String form(ProductOutDetail productOutDetail, Model model) {
		model.addAttribute("productOutDetail", productOutDetail);
		return "modules/ka/productOutDetailForm";
	}

	@RequiresPermissions("ka:productOutDetail:edit")
	@RequestMapping(value = "save")
	public String save(ProductOutDetail productOutDetail, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, productOutDetail)){
			return form(productOutDetail, model);
		}
		productOutDetailService.save(productOutDetail);
		addMessage(redirectAttributes, "保存货品出库明细成功");
		return "redirect:"+Global.getAdminPath()+"/ka/productOutDetail/?repage";
	}
	
	@RequiresPermissions("ka:productOutDetail:delete")
	@RequestMapping(value = "delete")
	public String delete(ProductOutDetail productOutDetail, RedirectAttributes redirectAttributes) {
		productOutDetailService.delete(productOutDetail);
		addMessage(redirectAttributes, "删除货品出库明细成功");
		return "redirect:"+Global.getAdminPath()+"/ka/productOutDetail/?repage";
	}
	
	@RequiresPermissions("ka:productOutDetail:view")
	@RequestMapping(value = "logisticsForm")
	public String logisticsForm(ProductOutDetail productOutDetail, Model model) {
		model.addAttribute("productOutDetail", productOutDetail);
		return "modules/ka/productLogisticsForm";
	}
	
	@RequiresPermissions("ka:productOutDetail:edit")
	@RequestMapping(value = "updateLogistics")
	public String updateLogistics(ProductOutDetail productOutDetail, Model model, RedirectAttributes redirectAttributes) {
		productOutDetailService.updateLogistics(productOutDetail);
		addMessage(redirectAttributes, "保存物流信息成功");
		return "redirect:"+Global.getAdminPath()+"/ka/productOutDetail/?repage";
	}
	
	
	@RequiresPermissions("ka:productOutDetail:view")
	@RequestMapping(value = "productReturnForm")
	public String productReturnForm(ProductOutDetail productOutDetail, Model model) {
		model.addAttribute("productOutDetail", productOutDetail);
		return "modules/ka/productReturnForm";
	}
	
	@RequiresPermissions("ka:productOutDetail:edit")
	@RequestMapping(value = "saveSalesReturn")
	public String saveSalesReturn(ProductOutDetail productOutDetail, Model model, RedirectAttributes redirectAttributes) {
		
		int cnt = productOutDetailService.getCount(productOutDetail);//是否超出退货数量
		
		if(cnt==0){
			model.addAttribute("message", "超出退货数量");
			return productReturnForm(productOutDetail, model);
		}
		
		productOutDetailService.saveSalesReturn(productOutDetail);
		addMessage(redirectAttributes, "保存退货信息成功");
		return "redirect:"+Global.getAdminPath()+"/ka/productOutDetail/?repage";
	}

	/**
	 * 导出货品出库数据
	 * @param user
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("ka:productOutDetail:view")
    @RequestMapping(value = "export", method=RequestMethod.POST)
    public String exportFile(ProductOutDetail productOutDetail, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "货品出库数据"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            List<ProductOutDetail> list = productOutDetailService.findProductOutDetail(productOutDetail);
    		new ExportExcel("货品出库数据", ProductOutDetail.class).setDataList(list).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出货品出库数据失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/ka/salesReturn/?repage";
    }
}